<template>
	<view class="container">
		<view class="cu-bar">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="输入需要搜索的内容" confirm-type="search" @input="changeKeyWord" @confirm="loadData()"></input>
			</view>
		</view>
		<view class="view_order_by">
			<image src="../../static/icon_menu.png" class="iv_switch_list" @click="switchMenu"></image>
			<text class="tv_order_by" @click="switchSort">{{orderBy}}</text>
			<image src="../../static/icon_down_narrow.png" class="iv_down"></image>
		</view>
		<view class="view_line"></view>

		<uni-swipe-action class="uni-list" v-if="isListView">
			<uni-swipe-action-item :rightOptions="options" v-for="(item,index) in list" :key="index"
				@click="deleteNoteFun($event,item)">
				<view class="uni-list-cell-navigate" @click="editNote(item)">
					<view class="item_title">{{item.title}}</view>
					<view class="item-content">
						<view class="item_time">
							{{item.update_time}}
						</view>
						<view class="item_content">
							{{item.content}}
						</view>
					</view>
					<view class="view_line_2"></view>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>

		<view v-if="!isListView" style="width: 100%;padding-bottom: 80rpx;flex: 1;">
			<view style="width:90%;margin-left:5%;margin-right: 5%;margin-top: 20rpx;height: auto;"
				v-for="(item,index) in list" @click="gridClick(item)" :key="index">
				<view class="view_note_grid">
					<view class="text_title_grid">{{item.title}}</view>
					<view class="text_content_grid">{{item.content}}</view>
					<view class="text_time_grid">{{item.update_time}}</view>
				</view>
			</view>
		</view>
		<view class="bottom_view">
			<view class="view_count">
				{{list.length}}个备忘录
			</view>
			<image src="../../static/icon_write_note.png" @click="toWritePage()" class="iv_write"></image>
		</view>
	</view>
</template>

<script>
	import {
		isOpenDB,
		openDB,
		createTable,
		selectSQL,
		droptable,
		closeDB,
		deleteNote,
	} from './sqlite/sqlite.js'
	export default {
		data() {
			return {
				isListView: false,
				title: 'Hello',
				orderBy: '按创建日期排序',
				list: [{
					title: "hahahah",
					content: "哈哈哈还"
				}],
				swipeList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: 'rgb(254, 1, 34)'
					}
				}],
				screenWidth: "",
				screenHeight: "",
				orderbyItemList: ["按创建日期排序", "按编辑日期排序", "按标题排序"],
				orderbyIndex: 1,
				keyWord: "",
			}
		},
		onShow() {
			this.loadData()
		},
		onLoad() {
			this.title = 'hello uniapp'
			uni.setNavigationBarTitle({
					title: '备忘录'
				}),
				uni.getSystemInfo({
					success: function(res) {
						this.screenWidth = res.screenWidth
						this.screenHeight = res.screenHeight
					}
				})

		},
		methods: {
			changeKeyWord(e){
				this.keyWord=e.detail.value;
			},
			switchMenu() {
				this.isListView = !this.isListView;
			},
			loadData() {
				selectSQL(this.keyWord, this.orderbyIndex, res => {
					this.list = res
				})
			},
			toWritePage() {
				uni.navigateTo({
					url: 'writeNote/writeNote?type=1'
				})
			},
			editNote(note) {
				uni.navigateTo({
					//对象如果不转成字符串的话就会变成[object,object]这个形式
					url: 'writeNote/writeNote?type=2&note=' + JSON.stringify(note)
				})
			},
			deleteNoteFun(e, note) {

				console.log(JSON.stringify(e));
				deleteNote(note, res => {
					this.loadData()
				})

			},
			gridClick(note) {
				var that = this;
				uni.showActionSheet({
					itemList: ["编辑", "删除"],
					success(res) {
						if (res.tapIndex == 0) {
							that.editNote(note)
						} else if (res.tapIndex == 1) {
							that.deleteNoteFun(null, note)
						}
					},
					fail(res) {
						console.log(res)
					}

				})
			},
			switchSort() {
				var that = this;
				uni.showActionSheet({
					itemList: that.orderbyItemList,
					success(res) {
						that.orderbyIndex = res.tapIndex + 1;
						that.loadData();
					},
					fail(res) {
						console.log(res)
					}

				})
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: white;
		height: 100%;
		width: 100%;
		display: flex;
	}

	.container {
		display: flex;
		flex-direction: column;
		width: 100%;
		margin: 0px;
		padding-top: 10rpx;
		height: 100%;
	}

	.view_order_by {
		width: 100%;
		height: auto;
		margin-top: 20rpx;
		padding: 20rpx;
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.cu-bar {
		padding: 0px;
		height: max-content;
	}

	.iv_switch_list {
		width: 50rpx;
		height: 50rpx;
	}

	.iv_down {
		width: 50rpx;
		height: 50rpx;

	}

	.tv_order_by {
		width: max-content;
		font-size: 28rpx;
		color: $color_godden_dark;
		flex: 1;
		text-align: right;
	}

	.view_line {
		width: 90%;
		height: 1rpx;
		background-color: $color_line;
		margin-left: 5%;
		margin-right: 5%;
	}

	/**
	 * 去掉item的尾部分割线
	 */
	.uni-list-cell::after {
		border: none;
		height: 0rpx;
	}

	/**
	* 去掉列表头部的线
	*/
	.uni-list::after {
		border: none;
		border-top-width: 0rpx;
		height: 0rpx;
	}

	.uni-list {
		width: 100%;
		display: flex;
		flex: 1;
	}

	/**
	 * 去掉列表的尾部分割线
	 */
	.uni-list::before {
		border: none;
		border-top-width: 0rpx;
		height: 0rpx;
	}

	.uni-list-cell {
		width: max-content;
		flex-direction: row;
		height: auto;
	}

	.uni-list-cell-navigate {
		display: flex;
		flex: 1;
		margin-left: 5%;
		margin-right: 5%;
		width: 90%;
		flex-direction: column;
		align-items: flex-start;
		position: inherit;
		margin-top: 10rpx;
		padding: 0rpx;
	}


	.item-content {
		display: flex;
		flex-direction: row;
	}

	.item_time {
		color: #999999;
		width: auto;
		white-space: nowrap;
		font-size: 30rpx;
	}

	.item_content {
		color: #C0C0C0;
		font-size: 30rpx;
		margin-left: 20rpx;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-break: break-all;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
	}

	.item_title {
		flex: 1;
		display: flex;
		font-weight: bold;
		font-size: 35rpx;
		color: black;
	}

	.view_line_2 {
		width: 100%;
		height: 1rpx;
		background-color: $color_line;
		margin-top: 20rpx;
	}

	.bottom_view {
		width: 100%;
		height: 80rpx;
		align-items: center;
		justify-content: center;
		display: flex;
		flex-direction: row;
	}

	.view_count {
		width: max-content;
		font-size: 30rpx;
		float: right;
		height: max-content;
	}

	.iv_write {
		width: 60rpx;
		height: 60rpx;
		justify-self: flex-end;
		position: absolute;
		right: 20rpx;
	}


	.view_note_grid {
		width: 32%;
		height: 150rpx;
		padding: 2%;
		margin-left: 1%;
		float: left;
		margin-top: 1%;
		display: flex;
		flex-direction: column;
		border: 1rpx solid #999999;
		border-radius: 10rpx;
	}

	.text_title_grid {
		width: 100%;
		font-weight: bold;
		font-size: 20rpx;
		color: #666666;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-break: break-all;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
	}

	.text_content_grid {
		width: 100%;
		font-weight: bold;
		font-size: 20rpx;
		color: #666666;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-break: break-all;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.text_time_grid {
		width: 100%;
		font-weight: bold;
		font-size: 20rpx;
		color: #666666;
	}
</style>
